package com.xhx.api.controller.v1.mongodb;

import com.alibaba.fastjson.JSON;
import com.xhx.dao.mongodb.GisCoordinate;
import com.xhx.model.utils.PageResult;
import com.xhx.model.utils.PagingModel;
import com.xhx.service.mongodb.GisCoordinateService;
import com.xhx.utils.JsonResult;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Qualifier;
import org.springframework.web.bind.annotation.*;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * GIS 控制器
 */
@RestController("V1GisCoordinateController")
@RequestMapping("v1/gis")
public class GisCoordinateController {
    private static final Logger log = LoggerFactory.getLogger(GisCoordinateController.class);

    @Autowired
    @Qualifier("gisCoordinateService")
    private GisCoordinateService gisCoordinateService;

    /**
     * 分页 指定检索条件
     *
     * @param page        当前页
     * @param size        记录数
     * @param sort        排序字段
     * @param order       排序
     * @param filterModel 过滤模型
     * @return JSON数据
     */
    // POST /v1/gis/list?page=1&size=10&sort=name&order=desc
    @PostMapping(value = "list")
    public JsonResult getAllGisCoordinate(
            @RequestParam(name = "page", defaultValue = "1") int page,
            @RequestParam(name = "size", defaultValue = "10") int size,
            @RequestParam(name = "sort", defaultValue = "id") String sort,
            @RequestParam(name = "order", defaultValue = "desc") String order,
            @RequestBody GisCoordinate filterModel
    ) {

        // 分页参数
        page = page < 1 ? 1 : page;
        size = size < 1 ? 1 : size;

        PagingModel pagingModel = new PagingModel();
        pagingModel.page = page;
        pagingModel.size = (size > 1000 ? 1000 : size);

        // 配置条件
        //filterModel.setGisUser(1);

        // 测试日志
        log.info("==========当前页【{}】检索条件=========={}", pagingModel.page, JSON.toJSONString(filterModel));

        // 当前页数据
        List<GisCoordinate> dataModels = this.gisCoordinateService.getAllGisCoordinate(pagingModel, filterModel);

        // 组装响应数据
        JsonResult jsonResult = new JsonResult();
        jsonResult.setCode(0); // 状态码
        jsonResult.setMsg("ok"); // 消息
        jsonResult.setData(new PageResult(pagingModel.count, pagingModel.page, pagingModel.size, dataModels)); // 业务数据

        // 输出响应结果
        return jsonResult;
    }

    /**
     * 添加
     *
     * @param gisCoordinate 实体
     * @return
     */
    @PostMapping(value = "")
    public long addGisCoordinate(@RequestBody GisCoordinate gisCoordinate) {
        // 级联
        return this.gisCoordinateService.addGisCoordinate(gisCoordinate);
    }

    @PostMapping(value = "test")
    public void addGisCoordinateTest(@RequestBody GisCoordinate gisCoordinate) {
        for (int i = 0; i < 10000; i++) {
            this.gisCoordinateService.addGisCoordinate(gisCoordinate);
        }
    }

    /**
     * 删除
     *
     * @param id 主键
     */
    @DeleteMapping(value = "{id}")
    public long deleteGisCoordinateById(@PathVariable(name = "id") long id) {
        // 级联
        return this.gisCoordinateService.deleteGisCoordinateById(id);
    }

    /**
     * 修改
     *
     * @param gisCoordinate
     */
    @PutMapping(value = "{id}")
    public long updateGisCoordinateById(@PathVariable Long id, @RequestBody GisCoordinate gisCoordinate) {
        // 级联
        return this.gisCoordinateService.updateGisCoordinateById(gisCoordinate);
    }

    /**
     * 查找
     *
     * @param id 主键
     * @return
     */
    @GetMapping(value = "{id}")
    public JsonResult getGisCoordinateById(@PathVariable long id) {
        // 单条记录
        GisCoordinate dataModel = this.gisCoordinateService.getGisCoordinateById(id);

        // 无此记录
        if (id != 0 && dataModel == null) {
            return new JsonResult(-1, "无此记录", id);
        }
        // 用于初始化
        else if (id == 0) {
            dataModel = new GisCoordinate();
        }

        // 组装多项数据
        Map<String, Object> map = new HashMap<>();
        map.put("detail", dataModel); // 表实体模型

        // 输出 Json
        return new JsonResult(0, "ok", map);
    }

}
